$(() => {
    // 获取area_id
    let goods_id = GetQueryString('goods_id');
    let token = GetQueryString('token');
    let area_code = '';
    ajaxList(area_code);

    //N类商品详情
    function ajaxList(area_code) {
        $.ajax({
            type: "post",
            url: url,
            data: { 'method': "Plat|ngoods_detail", 'goods_id': goods_id, 'area_code': area_code },
            dataType: 'json',
            success: function(res) {
                // console.log(res);
                if (res.status == 1) {
                    let company_list = res.data.company_list;
                    let goods_info = res.data.goods_info;
                    let banner = '';
                    let html = '';

                    // 分享
                    let description = goods_info.description;
                    let goods_img = goods_info.goods_thumb;
                    let goods_name = goods_info.goods_name;
                    let goods_url = domain + '/view/share_n_goodsDetail.html?goods_id=' + goods_id;

                    // 商品名称
                    $('.hetitle').html(goods_name);

                    // 分享
                    $('.share').on('click', function() {
                        if (isAndroid_ios()) {
                            //安卓  
                            window.android.shareWebMethod(goods_url, goods_name, description, goods_img);
                        } else {
                            //ios
                            // window.webkit.messageHandlers.getCacheSize.postMessage(goods_url, goods_name, description, goods_img);
                            var d = {};
                            d.shareUrl = goods_url;
                            d.title = goods_name;
                            d.description = description;
                            d.imgUrl = goods_img;
                            window.webkit.messageHandlers.getCacheSize.postMessage(d);
                        }
                    })

                    // 商品信息
                    $('.description').html(goods_info.description);
                    let price = '';
                    if (goods_info.price_show_model == 1) {
                        price = `
                            <span>¥${goods_info.goods_price}</span>
                        `;
                    } else if (goods_info.price_show_model == 2) {
                        price = `
                            <del style="color:#666;font-size:.24rem;">¥${goods_info.price_before}</del>/<span style="font-size:.28rem">¥${goods_info.goods_price}</span>
                        `;
                    } else {
                        price = `
                            <span>¥${goods_info.price_area}</span>
                        `;
                    }
                    $('.goods_price').html(price);
                    $('.read_count').html('浏览量' + goods_info.read_count);
                    goods_info.pic.forEach(item => {
                        banner += `
                            <div class="swiper-slide"><img src="${item.thumb_url == '' ? '../img/banner.png' : item.thumb_url}" alt="${item.pic_name}" data-url="${item.pic_url}" class="baimg carBanner" /></div>
                        `;
                    })
                    $('.swiper-wrapper').html(banner);
                    // banner轮播
                    if (goods_info.pic.length == 1) {
                        new Swiper('.adBanner', {
                            autoplay: 3000,
                            // pagination: '.swiper-pagination',
                            loop: false,
                        })
                    } else {
                        new Swiper('.adBanner', {
                            autoplay: 3000,
                            // pagination: '.swiper-pagination',
                            loop: true,
                        })
                    }

                    // banner放大轮播
                    $('.carBanner').on('click', function() {
                        $('.mask').css('background', 'black');
                        let idx = $(this).parent().attr('data-swiper-slide-index');
                        console.log(idx);
                        dialog('#plusBanner');
                        let html = '';
                        goods_info.pic.forEach(item => {
                            html += `
                                <div class="swiper-slide">
                                    <img class="showImg" src="${item.pic_url}" alt="">
                                </div>
                            `;
                        })
                        $('.plusBannerSwiper .swiper-wrapper').html(html);
                        let plusBanner = new Swiper('.plusBannerSwiper', {
                            // pagination: '.swiper-pagination',
                            paginationType: 'fraction'
                        })
                        plusBanner.slideTo(idx, false);
                    })


                    company_list.forEach(item => {
                        html += `
                        <div class="comList">`
                        if (item.company_level >= 1) {
                            html += `<a href="shop_details_company.html?company_id=${item.id}&token=${token}" class="comHead">`
                        } else {
                            html += `<a href="shop_details.html?company_id=${item.id}&token=${token}" class="comHead">`
                        }
                        html += `<div class="comHeadLeft">
                                    <img class="headImg" src="${item.company_logo == '' ? '../img/user.jpg' : item.company_logo}" alt="${item.company_name}">
                                    <div class="headlineTopChild">
                                        <p>${item.company_name}</p>`
                        if (item.level_logo != '') {
                            html += `<img src="${item.level_logo}" alt="${item.level_name}" class="userLevel"></img>`
                        }
                        html += `</div>
                                </div>
                                <p class="comHeadRight">进店</p>
                        </a>
                            <div class="comImgs">`
                        item.goods_list.forEach(goods => {
                            html += `<a href="comp_shopDetail.html?id=${goods.id}&token=${token}"><img class="comImg plusImg" data-url="${goods.goods_img}" src="${goods.goods_thumb == '' ? '../img/user.jpg' : goods.goods_thumb}" alt="商品图片"></a>`;
                        })
                        html += `</div>
                            <p class="comDesc">
                                主营：${item.main_business}
                            </p>
                            <p class="comAddr">
                                地址：${item.area_name}
                                <span>浏览量 ${item.read_count}</span>
                            </p>
                        </div>
                        `;
                    });
                    if (html != '') {
                        $('.comLists').html(html);
                    } else {
                        $('.comLists').html(`
                            <p class="h88" style="color:rgb(128, 128, 128);text-align:center;">暂无更多信息！！！</p>
                        `);
                    }

                } else if (res.status == 2) {
                    location.href = "fox://logout";
                } else {
                    alert(res.message);
                }
            }
        })
    }

    // 地区选择
    $('.cityBtn').on('click', function() {
        let num = $(this).attr("data-num");
        if (num == 1) {
            $(this).attr("data-num", 2);
            $('.cityBtn img')[0].style.transform = 'rotate(180deg)';
        } else {
            $(this).attr("data-num", 1);
            $('.cityBtn img')[0].style.transform = 'rotate(0deg)';
            dialogClose();
        }
        $.ajax({
            type: "post",
            url: url,
            data: { 'method': "Api|get_area_list", 'pcode': 1 },
            dataType: 'json',
            success: function(res) {
                // // console.log(res);
                let data = res.data;
                let html = '';
                let all = '';
                all = `
                    <a href="javascript:;" data-code="" class="h88 cityItem">全部</a>
                `;
                if (res.status == 1) {
                    data.forEach(item => {
                        html += `
                            <a href="javascript:;" data-code="${item.area_code}" class="h88 cityItem">${item.area_name}</a>
                         `;
                    })
                    $('.cityLists').html(all + html);
                }
                $('.cityItem').on('click', function() {
                    let area_code = $(this).attr('data-code');
                    if (area_code == '') {
                        $('.city_area').html('地区');
                        ajaxList(area_code);
                    } else {
                        let city_name = $(this).html();
                        $('.city_name').attr('data-city_name', city_name);
                        if (area_code) {
                            ajaxList(area_code);
                            let city_area = $('.city_name').attr('data-city_name');
                            $('.city_area').html(city_area);
                        }
                    }
                    $('.cityBtn img')[0].style.transform = 'rotate(0deg)';
                    dialogClose();
                })
            }
        })
    })
})